package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.LearningExperienceListener;
import com.TeacherInfoServer.pojo.DTO.LearningExperienceDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.LearningExperienceSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateLearningExperienceDTO;
import com.TeacherInfoServer.pojo.excelImport.LearningExperienceExcel;
import com.TeacherInfoServer.common.service.LearningExperienceService;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s1/learningExperience")
@Tag(name = "教师学习经历信息控制器",description = "用于对教师学习经历信息的控制")
public class LearningExperienceController {
    @Autowired
    public LearningExperienceService learningExperienceService;
    @Autowired
    public LearningExperienceListener learningExperienceListener;

    @Operation(summary = "获取教师学习经历信息列表",description = "分页获取教师学习经历信息列表")
    @GetMapping("/getList")
    public Result<Map<String,Object>> getAllList(@RequestParam("pages") @Parameter(description = "当前页数") Integer pages,
                                                 @RequestParam("size") @Parameter(description = "当前页数显示信息的数量") Integer size){
        return Result.success("获取历任主官信息列表成功",learningExperienceService.getList(pages,size));
    }
    @Operation(summary = "模糊查询获取教师学习经历信息列表",description = "模糊查询分页获取教师学习经历信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody LearningExperienceSearchDTO learningExperienceSearchDTO){
        return Result.success("获取教师学习经历列表成功",learningExperienceService.searchLearningExperienceList(learningExperienceSearchDTO));
    }

    @Operation(summary = "添加单个教师学习经历信息",description = "添加单个教师学习经历信息")
    @PostMapping("/addOne")
    public Result<?> addOne(@RequestBody @Parameter(description = "教师学习经历DTO类")LearningExperienceDTO learningExperienceDTO){
        learningExperienceService.addLearningExperience(learningExperienceDTO);
        return Result.success("添加教师学习经历信息成功");
    }

    @Operation(summary = "更新单个教师学习经历信息",description = "更新教师学习经历信息")
    @PutMapping("/updateOne")
    public Result<?> updateOne(@RequestBody @Parameter(description = "教师学习经历类") UpdateLearningExperienceDTO learningExperience){
        learningExperienceService.updateLearningExperience(learningExperience);
        return Result.success("修改教师学习经历信息成功");
    }

    @Operation(summary = "根据id批量删除教师学习经历信息",description = "根据id批量删除教师学习经历信息")
    @DeleteMapping("/deleteLearningExperience")
    public Result<?> deleteDepartment(@RequestParam("ids") @Parameter(description = "选择删除的教师学习经历id列表") List<Long> ids){
        boolean isDelete=learningExperienceService.deleteLearningExperience(ids);
        if (isDelete) {
            return Result.success("删除成功");
        }
        else {
            return Result.fail("没有相应教师学习经历信息，删除失败");
        }
    }


    @Operation(summary = "Excel批量导入教师学习经历信息",description = "Excel批量导入教师学习经历信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String,Integer>> addBatch(@RequestParam("file")@Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {
        learningExperienceListener.initList();
        EasyExcel.read(file.getInputStream(), LearningExperienceExcel.class,learningExperienceListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",learningExperienceListener.getRecord());
        return Result.success(200,"批量导入成功",map);
    }

    @Operation(summary = "Excel导出教师学习经历信息",description = "Excel导出教师学习经历信息")
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
        learningExperienceService.exportInfo(response);
    }
}
